package com.example.medmobile.profissional;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import utils.BaseActivity;
import utils.MyApplication;
import WebService.SoapWebService;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.example.medmobile.R;

public abstract class AgendaProfissional extends BaseActivity {
	protected ListView listView;
	protected List<com.google.appengine.medmobileserver.business.Agendamento> li;

	protected ProgressDialog dialog;
	protected Handler handler = new Handler();

	private int mYear;
	private int mMonth;
	private int mDay;
	private EditText filtroBusca;
	static final int DATE_DIALOG_ID = 0;

	private Dialog dia;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_agenda_profissional);
		listView = (ListView) findViewById(R.id.listaAgendaProfissional);
		filtroBusca = (EditText) findViewById(R.id.AgendaProfissionalFiltroData);
		Calendar c = Calendar.getInstance();
		filtroBusca.setText(new SimpleDateFormat("dd/MM/yyyy").format(
				c.getTime()).toString());
		this.mYear = c.get(Calendar.YEAR);
		this.mMonth = c.get(Calendar.MONTH);
		this.mDay = c.get(Calendar.DAY_OF_MONTH);

		filtroBusca.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				showDialog(DATE_DIALOG_ID);
			}
		});

		CarregaLista(new SimpleDateFormat("ddMMyyyy").format(c.getTime())
				.toString(), this);

		Button btConfirmar = (Button) findViewById(R.id.btAgendaProfissionalConfirmar);
		Button btCancelar = (Button) findViewById(R.id.btAgendaProfissionalCancelar);

		btConfirmar.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				new Thread() {
					public void run() {
						for (int i = 0; i < listView.getChildCount(); i++) {

							final View v1 = listView.getChildAt(i);
							CheckBox cb = (CheckBox) v1
									.findViewById(R.id.chAgendamentoSelecionado);

							if (cb.isChecked()) {

								Integer idAgendamento = (Integer) v1.getTag();
								String sRetorno = SoapWebService.ConfirmarAgendamento(idAgendamento);
								if (sRetorno.equals("Agendamento confirmado com sucesso.")) {
									new Thread() {
										public void run() {
											MyApplication.currentActivity().runOnUiThread(
											new Runnable() {
											public void run() {
												TextView textStatus = (TextView) v1.findViewById(R.id.AdapterAgendamentoStatusConsulta);
												textStatus.setText("Confirmado");
												textStatus.setTextColor(AgendaProfissional.this.getResources().getColor(R.color.verde));
														}
												});

										}
									}.start();

								}

							}

						}

					}
				}.start();
			}
		});

		btCancelar.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				new Thread() {
					public void run() {

						for (int i = 0; i < listView.getChildCount(); i++) {
							final View v1 = listView.getChildAt(i);
							CheckBox cb = (CheckBox) v1
									.findViewById(R.id.chAgendamentoSelecionado);

							if (cb.isChecked()) {
								Integer idAgendamento = (Integer) v1.getTag();
								String sRetorno = SoapWebService
										.CancelarAgendamentoProfissional(idAgendamento);
								
								if (sRetorno.equals("Agendamento cancelado com sucesso.")) {
									new Thread() {
										public void run() {
											MyApplication.currentActivity().runOnUiThread(
											new Runnable() {
											public void run() {
												TextView textStatus = (TextView) v1.findViewById(R.id.AdapterAgendamentoStatusConsulta);
												textStatus.setText("Cancelado");
												textStatus.setTextColor(AgendaProfissional.this.getResources().getColor(R.color.vermelho));
														}
												});

										}
									}.start();

								}
							}
						}

					}
				}.start();

			}
		});

	}

	protected abstract void CarregaLista(String sData, Context context);

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.agenda_profissional, menu);
		return true;
	}

	// the callback received when the user "sets" the date in the dialog
	private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			
			if (mYear == year && mMonth == monthOfYear + 1
					&& mDay == dayOfMonth)
				return;

			
			mYear = year;
			mMonth = monthOfYear + 1;
			mDay = dayOfMonth;

			NumberFormat formatter = new DecimalFormat("00");
			filtroBusca.setText(formatter.format(mDay) + "/"
					+ formatter.format(mMonth) + "/" + mYear);
			String sData = formatter.format(mDay) + formatter.format(mMonth)
					+ mYear;
			CarregaLista(sData, AgendaProfissional.this);

		}
	};

	@Override
	protected Dialog onCreateDialog(int id) {
		DatePickerDialog d = null;
		switch (id) {
		case DATE_DIALOG_ID:
			d = new DatePickerDialog(this, mDateSetListener, this.mYear,
					this.mMonth, this.mDay);

		}

		d.updateDate(mYear, mMonth, mDay);

		return d;
	}

}
